<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Released under the MIT License.
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------

namespace app\app\controller;

use cmf\controller\AdminBaseController;
use think\Db;
use app\admin\model\AdminMenuModel;
use think\Validate;
use api\wxapp\model\User;
use api\wxapp\model\Address;
class AddressController extends AdminBaseController
{
    private $shop_id;
    private $where = array();
    public function initialize()
    {
        parent::initialize();
        $sessionAdminId = session('ADMIN_ID');
        if($sessionAdminId !== 1){
            //说明是店铺登陆
            $find = db::name('app_shop')->where('admin_id',$sessionAdminId)->find();
            $this->shop_id = $find['id'];
            $this->where[] = ['shop_id','eq',$find['id']];
        }
    }
    public function index(Address $address)
    {
        $where = array();
        $params = $this->param();
        if($this->shop_id){
            $idarr = User::where('shop_id',$this->shop_id)->column('id');
            $where[] = ['t1.uid','in',$idarr];
        }
        if(isset($params['nickname'])){
            $where[] = ['t2.nickname','like','%'.$params['nickname'].'%'];
        }
        $query = Address::alias('t1');
        $query = $address->leftuser($query);
        $query = $address->returnfield($query);
        $list = $query->where($where)->paginate(10);
        $this->assign('list',$list);
        $list->appends($params);  //分页条件
        $this->assign('page', $list->render());
        return $this->fetch();
    }
    //添加试题
    public function add()
    {
        if($_POST){
            $validate = new Validate([
                'name'  => 'require',
            ]);
            if(!$validate->check($_POST)){
                $this->error($validate->getError());
            }
            if(!$_POST['answer']['A'] || !$_POST['answer']['B'] || !$_POST['answer']['C'] || !$_POST['answer']['D']){
                $this->error('请填入答案');
            }
            $_POST['answer'] = json_encode($_POST['answer']);
            $_POST['addtime'] = date('Y-m-d H:i:s',time());
            $res = Skin::insert($_POST);
            if($res){
                $this->success('添加成功');
            }else{
                $this->error('添加失败');
            } 
        }
        return $this->fetch();
    }
    //试题编辑
    public function edit()
    {
        $params = $this->param();
        if($_POST){
            $validate = new Validate([
                'name'  => 'require',
            ]);
            if(!$validate->check($_POST)){
                $this->error($validate->getError());
            }
            if(!$_POST['answer']['A'] || !$_POST['answer']['B'] || !$_POST['answer']['C'] || !$_POST['answer']['D']){
                $this->error('请填入答案');
            }
            $_POST['answer'] = json_encode($_POST['answer']);
            $res = Skin::update($_POST);
            if($res){
                $this->success('成功');
            }else{
                $this->error('失败');
            } 
        }
        $info = Skin::get($params['id']);
        $info['answer'] = json_decode($info['answer'],true);
        $this->assign('info',$info);
        return $this->fetch();
    }
    //试题删除
    public function delete()
    {
        $params = $this->param();
        $res = Skin::destroy($params['id']);
        if($res){
            $this->success('成功');
        }else{
            $this->error('失败');
        }
    }
    //皮肤测试记录
    public function lst()
    {
        $where = array();
        $params = $this->param();
        if(isset($params['nickname'])){
            $where[] = ['t2.nickname','like','%'.$params['nickname'].'%'];
        }
        $sessionAdminId = session('ADMIN_ID');
        if($sessionAdminId !== 1){
            //说明是店铺登陆
            $idarr = db::name('app_shop')->alias('t1')
                ->join('app_user t2','t2.shop_id = t1.id')
                ->where('t1.admin_id',$sessionAdminId)
                ->column('t2.id');
            $where[] = ['t1.uid','in',$idarr];
        }
        $slist = SkinList::alias('t1')
            ->join('app_user t2','t1.uid = t2.id')
            ->where($where)
            ->field('t1.*,t2.nickname')
            ->paginate(10);
        $this->assign('slist',$slist);
        $slist->appends($params);  //分页条件
        $this->assign('page', $slist->render());
        return $this->fetch();
    }
    //用户答题记录详情
    public function slst()
    {
        $params = $this->param();
        $list = Skin::alias('t1')
            ->join('app_slist_info t2','t2.skin_id = t1.id')
            ->field('t1.name,t2.result,t2.addtime,t2.id,t1.answer')
            ->where('t2.skinlist_id',$params['id'])
            ->paginate(10,false,
                ['query'=>$params])->each(function($item, $key){
                $item['value'] = json_decode($item['answer'],true)[$item['result']];
                return $item;
            });
        $this->assign('list',$list);
        $this->assign('page', $list->render());   
        return $this->fetch();
    }
}
